package com.unknowpupil.web.base;

import com.unknowpupil.web.base.common.ResponseData;
import com.unknowpupil.web.base.common.ResultEnums;

import javax.servlet.http.HttpServletResponse;

/**
 * @Classname: BaseResult
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/9/28 12:23
 * @Description: 工厂模式根据需求返回类型 必须有接口，才能实现它的实现类
 *  抽象类和接口区别，接口里的方法都是抽象方法 抽象类也可以被继承 也类似与有了接口的能力因为他又多个实现多个子类 面向对象之多态
 * @Version: 1.0
 */
public class BaseResult<T extends AbstractBaseDomain> {
    private static final String LOGGER_LEVEL_DEBUG = "DEBUG";

    private static BaseResult baseResultFactory;

    /**
     * 私有构造，我不让你 new
     */
    private BaseResult(){
    }

    private static HttpServletResponse response;
    /**
     * 单例模式 实例化出   让controller 点击
     */
    public static BaseResult getInstance(HttpServletResponse response){
        if(baseResultFactory == null){
            synchronized (BaseResult.class){
                //双重锁机制，以求绝对单例
                if(baseResultFactory ==null){
                    baseResultFactory = new BaseResult();
                }
            }
        }
        BaseResult.response = response;
        baseResultFactory.initResponse();
        return baseResultFactory;
    }
    public static <T> ResponseData buildSuccess(T data) {
    return new ResponseData<T>(ResultEnums.SUCCESS, data);
}

    public static ResponseData buildSuccess() {
        return new ResponseData(ResultEnums.SUCCESS);
    }

    public static ResponseData buildSuccess(String msg) {
        return new ResponseData(ResultEnums.SUCCESS.getCode(), msg);
    }

    public static ResponseData buildSuccess(String code, String msg) {
        return new ResponseData(code, msg);
    }

    public static <T> ResponseData buildSuccess(String code, String msg, T data) {
        return new ResponseData<T>(code, msg, data);
    }

    public static ResponseData buildSuccess(ResultEnums resultEnums) {
        return new ResponseData(resultEnums);
    }

    public static <T> ResponseData buildError(T data) {
        return new ResponseData<T>(ResultEnums.ERROR, data);
    }

    public static ResponseData buildError() {
        return new ResponseData(ResultEnums.ERROR);
    }

    public static ResponseData buildError(String msg) {
        return new ResponseData(ResultEnums.ERROR.getCode(), msg);
    }

    public static ResponseData buildError(String code, String msg) {
        return new ResponseData(code, msg);
    }

    public static <T> ResponseData buildError(String code, String msg, T data) {
        return new ResponseData<T>(code, msg, data);
    }

    public static ResponseData buildError(ResultEnums resultEnums) {
        return new ResponseData(resultEnums);
    }
    /**
     * build default data, error information show detail that log level  is debug
     * @param code
     * @param title
     * @param detail
     * @param level
     * @return
     */
    public  ResponseData build(int code,String title,String detail,String level){
        //请求失败得响应码
        response.setStatus(code);
       if(LOGGER_LEVEL_DEBUG.equals(level)){
           return  BaseResult.buildError(code+"",title,detail);
       }
       return  BaseResult.buildError(code+"",title);

    }

    private void initResponse(){
        response.setHeader("Content-Type", "application/vnd.api+json");
    }

}
